/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hessingerbg.codebehind;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;

/**
 *
 * @author MikeLaptop
 */
public class routine implements Runnable {

    public boolean _run = false;
    private spellroutine sr = new spellroutine();
    private movementroutine mr = new movementroutine();
    JTextPane out;

    public routine(JTextPane outlog) {
        out = outlog;
        log.out = out;
    }
    
    public void Pause()
    {
        _run = false;
        mr.fullStop();
    }

    public void Resume()
    {
        try {
            log.start();
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            Logger.getLogger(routine.class.getName()).log(Level.SEVERE, null, ex);
        }
        sr = new spellroutine();
        mr = new movementroutine();
        _run = true;
        log.println("Starting Run Routine");
    }

    public void run()
    {
        long last = System.currentTimeMillis();
        while(true)
        {
            while(_run)
            {
                try {
                    synchronized (this) {
                        sr.doRoutine();
                        mr.doRoutine();
                        log.println("Casting Spell");
                        if(sr.requiresStop())
                        {
                            mr.fullStop();
                            double z = sr.requiredStopSeconds();
                            sr.useSpell();
                            this.wait((long) (z * 1000));
                        }
                        else
                        {
                            sr.useSpell();
                            //long x = System.currentTimeMillis() - last;
                            //if(x<1500)
                            this.wait(1500);
                        }
                        
                    }
                } catch (Exception ex) {
                    Logger.getLogger(routine.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

}
